package com.dzl.goods;

import com.dzl.base.controller.BaseController;
import com.dzl.goods.pojo.WelfareGoodsAttr;
import com.dzl.goods.pojo.dto.WelfareGoodsAttrDto;
import com.dzl.goods.pojo.vo.WelfareGoodsAttrVoucherVo;
import com.dzl.goods.service.WelfareGoodsAttrService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 商品sku规格表(WelfareGoodsAttr)表控制层
 * @author ChenLong
 * @date 2021-06-09 15:13:00
 */
@Slf4j
@RestController
@RequestMapping("/welfareGoodsAttr")
public class WelfareGoodsAttrController extends BaseController {

    @Autowired
    private WelfareGoodsAttrService service;

    /**
     * 获取商品规格列表
     * @param welfareGoodsAttrDto 规格获取实体
     * @return com.dzl.util.JsonResult
     * @author ChenLong
     * @since 2021-05-18 19:43:07
     **/
    @GetMapping(value = "getWelfareGoodsAttrInfo")
    public JsonResult<WelfareGoodsAttr> getWelfareGoodsAttrInfo(WelfareGoodsAttrDto welfareGoodsAttrDto) {
        log.info("getWelfareGoodsAttr in welfareGoodsAttrDto: " + welfareGoodsAttrDto.toString());
        WelfareGoodsAttr welfareGoodsAttr = new WelfareGoodsAttr();
        if (EmptyUtils.isEmpty(welfareGoodsAttrDto.getGoodsId())) {
            return JsonResult.paramErrWithMsg("参数错误");
        } else {
            welfareGoodsAttr.setGoodsId(welfareGoodsAttrDto.getGoodsId());
        }
        if (EmptyUtils.isEmpty(welfareGoodsAttrDto.getFirstDetailId())) {
            return JsonResult.paramErrWithMsg("参数错误");
        } else {
            welfareGoodsAttr.setFirstDetailId(welfareGoodsAttrDto.getFirstDetailId());
        }
        if (EmptyUtils.isNotEmpty(welfareGoodsAttrDto.getSecondDetailId())) {
            welfareGoodsAttr.setSecondDetailId(welfareGoodsAttrDto.getSecondDetailId());
        }
        if (EmptyUtils.isNotEmpty(welfareGoodsAttrDto.getThirdDetailId())) {
            welfareGoodsAttr.setThirdDetailId(welfareGoodsAttrDto.getThirdDetailId());
        }
        welfareGoodsAttr.setStatus(1);
        WelfareGoodsAttrVoucherVo welfareGoodsAttrRes = service.getGoodsAttrWithVoucher(welfareGoodsAttr);
        if (EmptyUtils.isEmpty(welfareGoodsAttrRes)) {
            return JsonResult.paramErrWithMsg("规格获取失败");
        }
        // 设置券密是否过期
        Date nowDate = new Date();
        if (EmptyUtils.isNotEmpty(welfareGoodsAttrRes.getVoucherExpiredTime()) && welfareGoodsAttrRes.getVoucherExpiredTime().compareTo(nowDate) < 0) {
            welfareGoodsAttrRes.setIsExpired(0);
        } else {
            welfareGoodsAttrRes.setIsExpired(1);
        }
        // 设置剩余库存，小程序端的库存数量等于现有量减去预占数量
        if (EmptyUtils.isNotEmpty(welfareGoodsAttrRes.getQuantity()) && EmptyUtils.isNotEmpty(welfareGoodsAttrRes.getLockQuantity())) {
            Integer remainQuantity = welfareGoodsAttrRes.getQuantity() - welfareGoodsAttrRes.getLockQuantity();
            if (remainQuantity < 0) {
                welfareGoodsAttrRes.setQuantity(0);
            } else {
                welfareGoodsAttrRes.setQuantity(welfareGoodsAttrRes.getQuantity() - welfareGoodsAttrRes.getLockQuantity());
            }
        }
        return JsonResult.success(welfareGoodsAttrRes);
    }
}
